package ic.distribution.service.impl;

import IC.common.core.domain.R;
import IC.common.core.web.page.TableDataInfo;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ic.distribution.config.OssUtil2;
import ic.distribution.domain.DTO.DefenseDTO;
import ic.distribution.domain.Defense;
import ic.distribution.domain.PO.DefensePO;
import ic.distribution.domain.VO.DefenseVo;
import ic.distribution.domain.Vehicle;
import ic.distribution.domain.result.PageResult;
import ic.distribution.domain.result.Result;
import ic.distribution.mapper.DefenseMapper;
import ic.distribution.remote.RemoteTofaceservie;
import ic.distribution.service.DesenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static IC.common.core.utils.PageUtils.startPage;

@Service
public class DesenseServiceImpl implements DesenseService {

    @Autowired
    private DefenseMapper mapper;

    @Autowired
    private RemoteTofaceservie remoteTofaceservie;

    @Override
    public void add(DefensePO defensePO) {
        // 取出布控人员url
        String url = defensePO.getDefensePerson();
        //  转base64
        String base64 = OssUtil2.imageUrlToBase64(url);
        // 去除前缀 转换为上传人脸参数格式
        String imageBase64 = base64.substring(20);
        String subject = defensePO.getDefensePersonName();
        // 远程调用上传人脸接口 上传布控人员
        R<String> defenseface = remoteTofaceservie.defenseface(imageBase64, subject);
        String data = defenseface.getData();
        Map<String,String> map = JSONObject.parseObject(data, Map.class);
        String imageId = map.get("image_id");

        try {
            defensePO.setImageId(imageId);
            defensePO.setDefenseCreatDate(new Date());
            //            布防名称不能为空
            if (defensePO.getDefenseName() == null || defensePO.getDefenseName().equals("")) {
                throw new RuntimeException("布防名称不能为空");
            }
            //            开始时间不能为空
            if (defensePO.getDefenseStartDate() == null || defensePO.getDefenseStartDate().equals("")) {
                throw new RuntimeException("开始时间不能为空");
            }
            //            结束时间不能为空
            if (defensePO.getDefenseEndDate() == null || defensePO.getDefenseEndDate().equals("")) {
                throw new RuntimeException("结束时间不能为空");
            }
            //            布防对象不能为空
            if (defensePO.getDefensePerson() == null || defensePO.getDefensePerson().equals("")) {
                throw new RuntimeException("请上传布防照片");
            }
            //            布防姓名不能为空
            if (defensePO.getDefensePersonName() == null || defensePO.getDefensePersonName().equals("")) {
                throw new RuntimeException("布防姓名不能为空");
            }
            //            身份证号不能为空
            if (defensePO.getDefenseCard() == null || defensePO.getDefenseCard().equals("")) {
                throw new RuntimeException("身份证号不能为空");
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startdate = dateFormat.parse(defensePO.getDefenseStartDate());
            Date enddate = dateFormat.parse(defensePO.getDefenseEndDate());
            if(enddate.before(startdate))
            {
                throw new RuntimeException("结束时间不能小于开始时间");
            }
            mapper.add(defensePO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void ScheduledDel() {
      //        查询数据库 判断当前时间大于defenseStartDate的情况下删除这条数据
        List<Defense> defense =mapper.ScheduledDel();
        System.out.println(defense);
    }

    @Override
    public PageInfo<Defense> desenselist(DefenseVo defenseVo) {
        PageHelper.startPage(defenseVo.getPageNum(), defenseVo.getPageSize());
        List<Defense> list = mapper.desenselist(defenseVo);
        PageInfo<Defense> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Defense> effective() {
        List<Defense> effective = mapper.effective();
        return effective;
    }

    @Override
    public void update(DefenseDTO defenseDTO) {
        try {

            mapper.update(defenseDTO);
        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("Invalid date format: " + e.getMessage(), e);
        }
    }

    @Override
    public void updstatu(Integer id) {
        Defense defense=mapper.finddefense(id);
        String subject = defense.getDefensePersonName();
        String imageId = defense.getImageId();
        remoteTofaceservie.deleteFaceBySubject(subject);
        mapper.updstatu(id);
    }

    @Override
    public Integer togetcount() {
        Integer togetcount = mapper.togetcount();
        return togetcount;
    }

    @Override
    public void cherkAll() {
        mapper.cherkAll();
    }

    @Override
    public void cherkPause() {
        mapper.cherkPause();
    }

    @Override
    public void updstatu2(Integer id) {
        mapper.updstatu2(id);
        Defense defensePO = mapper.finddefense(id);
        String url = defensePO.getDefensePerson();
        //  转base64
        String base64 = OssUtil2.imageUrlToBase64(url);
        // 去除前缀 转换为上传人脸参数格式
        String imageBase64 = base64.substring(20);
        String subject = defensePO.getDefensePersonName();
        // 远程调用上传人脸接口 上传布控人员
         remoteTofaceservie.defenseface(imageBase64, subject);
    }


}
